/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TCarSale;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.network.request.builder.OrderCarSaleRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.IResponse;
import com.gmail.blackdog1987.carcare.network.response.OrderCarSaleResponse;
import com.gmail.blackdog1987.carcare.network.response.OrderDriveCompanyResponse;
import com.gmail.blackdog1987.carcare.network.response.OrderMeirongResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.OrderCarSaleResponseHandler;
import com.gmail.blackdog1987.carcare.util.ModelUtil;

@EActivity(R.layout.activity_order_carsale)
public class OrderCarSaleActivity extends BaseActivity implements OnResponseListener, OnClickListener {
	private Context mContext;
	private TCarSale mCar;
	
	@ViewById(R.id.order_carsale_head)
	public CubeImageView mHead;
	@ViewById(R.id.order_carsale_title)
	public TextView mTitle;
	@ViewById(R.id.order_carsale_call)
	public View mCall;
	@ViewById(R.id.order_carsale_contact)
	public View mContact;
	@ViewById(R.id.order_carsale_location)
	public View mLocation;
	@ViewById(R.id.order_carsale_info)
	public TextView mInfo;
	@ViewById(R.id.order_carsale_price)
	public TextView mPrice;
	@ViewById(R.id.order_carsale_name)
	public EditText mName;
	@ViewById(R.id.order_carsale_phone)
	public EditText mPhone;
	@ViewById(R.id.order_carsale_content)
	public EditText mContent;
	@ViewById(R.id.order_carsale_cost)
	public TextView mCost;
	
	OrderCarSaleRequestBuilder mReq = new OrderCarSaleRequestBuilder();
	OrderCarSaleResponseHandler mHandler = new OrderCarSaleResponseHandler();
	
	@AfterViews
	public void afterViews() {
		mContext = this;
		initData();
		reg();
	}

	public void reg() {
		mHandler.setOnResponseListener(this);
	}

	public void initData() {
		mCar = (TCarSale) getIntent().getSerializableExtra("data");
		
		mTitle.setText(ModelUtil.getCarsaleTitle(mCar));
		mInfo.setText(ModelUtil.getCarsaleInfo(mCar));
		mPrice.setText(String.valueOf(mCar.getPrice()));
		mCost.setText(String.valueOf(mCar.getPrice()));
		if(mCar.getHeadurl()!=null&&mCar.getHeadurl().length()!=0){
			ImageLoader imageLoader = ImageLoaderFactory.create(mContext);
			App.loadImg(mHead, imageLoader, mCar.getHeadurl());
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case PayActivity.REQUESTCODE_PAY:
				Toast.makeText(mContext, "恭喜您支付成功", Toast.LENGTH_LONG).show();
				finish();
				break;
			}
		}
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		IResponse res = classz.getResponse();
		if(res instanceof OrderCarSaleResponse){
			Toast.makeText(mContext, "下单成功", Toast.LENGTH_LONG).show();
			setResult(Activity.RESULT_OK);
			finish();
//			TProduct product = new TProduct();
//			product.body = "二手车";
//			product.price = mCar.getPrice();
//			product.subject = "车拉丁";
//			product.table = "t_order_carsale";
//			product.tableId = ((OrderCarSaleResponse)classz.getResponse()).order.getId();
//			product.userid = new UserDao().getUser().id;
//			Intent i = new Intent(OrderCarSaleActivity.this, PayActivity.class);
//			i.putExtra("data", product);
//			startActivityForResult(i, PayActivity.REQUESTCODE_PAY);
		}
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "下单失败", Toast.LENGTH_LONG).show();
	}
	
	@Click(R.id.back)
	void back() {
		finish();
	}
	
	@Click(R.id.order_carsale_pay)
	void pay() {
		if (!App.validUser(this))
			return;
		mReq.setCarid(String.valueOf(mCar.getId())).setInfo(mContent.getText().toString()).setName(mName.getText().toString())
				.setPhone(mPhone.getText().toString()).setUserid("1").postJsonRequest(mContext, mHandler);
	}
}
